package com.shanghaionstar.home.adapter;

import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.support.v4.util.LruCache;
import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.onstar.cn.common.util.BitmapUtil;

import java.util.List;

/**
 * Created by Rengui Lu on 2016/6/23.
 */
public class LoadingPagerAdapter extends PagerAdapter {

    //界面列表
    private List<View> views;

    /* 内存缓存 */
    private LruCache<String, Bitmap> mMemoryCache;

    public LoadingPagerAdapter(List<View> views) {
        this.views = views;
        final int memoryCache =1;
        mMemoryCache = new LruCache<String, Bitmap>(memoryCache) {
            @Override
            protected int sizeOf(String key, Bitmap bitmap) {
                // 重写此方法来衡量每张图片的大小，默认返回图片数量。
                return bitmap.getByteCount() / 1024;
            }
        }; // 初始化
    }

    /**
     * @description 将bitmap添加到内存中去
     *
     * @param key
     * @param bitmap
     */
    public void addBitmapToMemoryCache(String key, Bitmap bitmap) {
        if (getBitmapFromMemCache(key) == null) {
            mMemoryCache.put(key, bitmap);
        }
    }

    /**
     * @description 通过key来从内存缓存中获得bitmap对象
     *
     * @param key
     * @return
     */
    private Bitmap getBitmapFromMemCache(String key) {
        return mMemoryCache.get(key);
    }

    @Override
    public void destroyItem(View container, int position, Object object) {
        View view = views.get(position);
        ((ViewGroup) container).removeView(view);
//        if (view instanceof ImageView) {
//            ImageView imageView = (ImageView) view;
//            BitmapUtil.recycleImageView(imageView);
////            releaseImageViewResouce(imageView);
//            ((ViewGroup) container).removeView(imageView);
//        } else {
//            view.setBackground(null);
//            ((ViewGroup) container).removeView(view);
//        }
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        View view = views.get(position);
        container.removeView(view);
//        if (view instanceof ImageView) {
//            ImageView imageView = (ImageView) view;
//            BitmapUtil.recycleImageView(imageView);
////            releaseImageViewResouce(imageView);
//            container.removeView(imageView);
//        } else {
//            view.setBackground(null);
//            container.removeView(view);
//        }
    }

    //获得当前界面数
    @Override
    public int getCount() {
        if (views != null) {
            return views.size();
        }
        return 0;
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        container.addView(views.get(position), 0);
        return views.get(position);
    }

    //判断是否由对象生成界面
    @Override
    public boolean isViewFromObject(View arg0, Object arg1) {
        return (arg0 == arg1);
    }

    public void releaseImageViewResouce(ImageView imageView) {
        if (imageView == null) return;
        Drawable drawable = imageView.getDrawable();
        if (drawable != null && drawable instanceof BitmapDrawable) {
            BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
            Bitmap bitmap = bitmapDrawable.getBitmap();
            if (bitmap != null && !bitmap.isRecycled()) {
                bitmap.recycle();
                bitmap = null;
            }
        }
        imageView.setImageBitmap(null);
        System.gc();
    }

    public void onFinish() {
        if (views != null && views.size() > 0) {
            for (View view : views) {
                if (view instanceof ImageView) {
                    ImageView imageView = (ImageView) view;
                    BitmapUtil.recycleImageView(imageView);
                    releaseImageViewResouce(imageView);
                } else {
                    view.setBackground(null);
                }
            }
        }
        System.gc();
    }
}
